﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ControlExplorer
{
    public partial class Template_Manage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string commandstring = "SELECT TOP 50 * FROM 模板列表 ORDER BY 内置模板 ASC, 修改时间 DESC";
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

            if (!IsPostBack)
            {
                using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
                {
                    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(commandstring, connection);
                    System.Data.OleDb.OleDbCommandBuilder builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);

                    DataTable dt = new DataTable();
                    adapter.Fill(dt);

                    FpSpread1.UseClipboard = false;
                    FpSpread1.ActiveSheetView.Protect = true;
                    FpSpread1.ActiveSheetView.AutoGenerateColumns = false;
                    FpSpread1.ActiveSheetView.DataAutoCellTypes = false;
                    FpSpread1.ActiveSheetView.DataSource = dt;
                    FpSpread1.ActiveSheetView.Columns[0].DataField = "模板编号";
                    FpSpread1.ActiveSheetView.Columns[1].DataField = "内置模板";
                    FpSpread1.ActiveSheetView.Columns[2].DataField = "模板类型";
                    FpSpread1.ActiveSheetView.Columns[3].DataField = "所属部门";
                    FpSpread1.ActiveSheetView.Columns[4].DataField = "模板名称";
                    FpSpread1.ActiveSheetView.Columns[5].DataField = "创建时间";
                    FpSpread1.ActiveSheetView.Columns[6].DataField = "修改时间";

                    // 添加过滤功能
                    FpSpread1.Sheets[0].AutoFilterMode = FarPoint.Web.Spread.AutoFilterMode.Enhanced;
                    FarPoint.Web.Spread.IRowFilter rowFilter = new FarPoint.Web.Spread.HideRowFilter(FpSpread1.Sheets[0]);
                    FarPoint.Web.Spread.FilterColumnDefinition fd1 = new FarPoint.Web.Spread.FilterColumnDefinition(1, FarPoint.Web.Spread.FilterListBehavior.Default);
                    FarPoint.Web.Spread.FilterColumnDefinition fd2 = new FarPoint.Web.Spread.FilterColumnDefinition(2, FarPoint.Web.Spread.FilterListBehavior.Default);
                    FarPoint.Web.Spread.FilterColumnDefinition fd3 = new FarPoint.Web.Spread.FilterColumnDefinition(3, FarPoint.Web.Spread.FilterListBehavior.Default);
                    FarPoint.Web.Spread.FilterColumnDefinition fd4 = new FarPoint.Web.Spread.FilterColumnDefinition(4, FarPoint.Web.Spread.FilterListBehavior.Default);
                    FarPoint.Web.Spread.FilterColumnDefinition fd5 = new FarPoint.Web.Spread.FilterColumnDefinition(5, FarPoint.Web.Spread.FilterListBehavior.Default);
                    FarPoint.Web.Spread.FilterColumnDefinition fd6 = new FarPoint.Web.Spread.FilterColumnDefinition(6, FarPoint.Web.Spread.FilterListBehavior.Default);

                    rowFilter.ColumnDefinitions.Add(fd1);
                    rowFilter.ColumnDefinitions.Add(fd2);
                    rowFilter.ColumnDefinitions.Add(fd3);
                    rowFilter.ColumnDefinitions.Add(fd4);
                    rowFilter.ColumnDefinitions.Add(fd5);
                    rowFilter.ColumnDefinitions.Add(fd6);

                    FpSpread1.Sheets[0].RowFilter = rowFilter;

                    for (int i = 0; i < FpSpread1.ActiveSheetView.RowCount; i++)
                    {
                        if (bool.Parse(FpSpread1.ActiveSheetView.Cells[i, 1].Value.ToString()))
                        {
                            FpSpread1.ActiveSheetView.Cells[i, 7].CellType = new FarPoint.Web.Spread.LabelCellType();
                        }
                    }
                }
            }
        }

        protected void FpSpread1_DataBinding(object sender, EventArgs e)
        {

        }

        protected void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
        {
            if (e.CommandName == "EditTemplate")
            {
                Response.Redirect("~/Template_Edit.aspx?tid=" + FpSpread1.ActiveSheetView.Cells[FpSpread1.ActiveSheetView.ActiveRow, 0].Text);
            }
            else if (e.CommandName == "FillData")
            {
                Response.Redirect("~/Template_Fill.aspx?tid=" + FpSpread1.ActiveSheetView.Cells[FpSpread1.ActiveSheetView.ActiveRow, 0].Text);
            }

        }

    }
}